<?php
	class Topic extends MY_controller{

		function __construct(){
			parent::__construct();
		}

		function index($subject_id = 0,$chapter_id = 0,$source_type = 0,$type = 0,$difficulty_level = 0,$subject_text = 0, $start_time = 0, $end_time = 0, $limit = 0){
			$this->load->model("Topics");
			$conditions = array();
			if($_POST){
				$post_data = $this->input->post();
				$subject_id = !empty($post_data['subject_id']) ? $post_data['subject_id'] : 0;
				$chapter_id = !empty($post_data['chapter_id']) ? $post_data['chapter_id'] : 0;
				$source_type = !empty($post_data['source_type']) ? $post_data['source_type'] : 0;
				$type = !empty($post_data['type']) ? $post_data['type'] : 0;
				$difficulty_level = !empty($post_data['difficulty_level']) ? $post_data['difficulty_level'] : 0;
				$subject_text = !empty($post_data['subject_text']) ? urlencode($post_data['subject_text']) : 0;
				$start_time = !empty($post_data['start_time']) ? $post_data['start_time'] : 0;
				$end_time = !empty($post_data['end_time']) ? $post_data['end_time'] : 0;
				$limit = 0;
			}
			$subject = $this->Topics->get_subject();

			$conditions['subject_id'] = $subject_id;
			$conditions['chapter_id'] = $chapter_id;
			$conditions['source_type'] = $source_type;
			$conditions['type'] = $type;
			$conditions['difficulty_level'] = $difficulty_level;
			$conditions['subject_text'] = urldecode($subject_text);
			$conditions['start_time'] = $start_time;
			$conditions['end_time'] = $end_time;
			$result = $this->Topics->get_topic_list($limit, $this->offset, $conditions);
			$this->load->library('pagination');
			$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			$page_config['base_url'] = '/topic/index/' . $subject_id . '/' . $chapter_id . '/' . $source_type . '/' . $type . '/' . $difficulty_level . '/' . $subject_text . '/' . $start_time . '/' . $end_time;
			$page_config['total_rows'] = $result['count'];
			$page_config['per_page'] = $this->offset;
			$page_config['uri_segment'] = 11;
			$this->pagination->initialize($page_config);
			$pagination = $this->pagination->create_links();
			$this->smarty->assign("current_url", $current_url);
			$this->smarty->assign("conditions", $conditions);
			$this->smarty->assign("topic_list", $result['result']);
			$this->smarty->assign("subject", $subject);
			$this->smarty->assign("pagination", $pagination);
			$this->smarty->assign("action", '题目列表');
			$this->smarty->display("topic/list.html");
		}

		private function save_analys($analys_data, $qid = 0){
			$data = array();
			foreach ($analys_data['analys_type'] as $key => $value) {
				if($value == 1){ //填空题
					if(!empty($analys_data['subject_text'][$key]) && !empty($analys_data['tk_answer_text'][$key])){
						$data[$key]['analys'] = array("analys_type" => $value, "subject_text" => $analys_data['subject_text'][$key]);
						$data[$key]['analys_result'][] = array("answer_text" => $analys_data['tk_answer_text'][$key]);
					}
				}else{//分录题
					if(!empty($analys_data['subject_text'][$key]) && !empty($analys_data['answer_text'][$key])){
						$data[$key]['analys'] = array("analys_type" => $value, "subject_text" => $analys_data['subject_text'][$key]);
						foreach ($analys_data['answer_text'][$key] as $k => $answer_text) {
							$answer_text = trim($answer_text);
							$type = $analys_data['type'][$key][$k];
							$topic_subject_id = $analys_data['topic_subject_id'][$key][$k];
							$topic_subject_title = $analys_data['topic_subject_title'][$key][$k];
							if(!empty($answer_text) && !empty($type) && !empty($topic_subject_id)){
								$data[$key]['analys_result'][] = array("answer_text" => $answer_text, "type" => $type, "topic_subject_id" => $topic_subject_id, "topic_subject_title" => $topic_subject_title);
							}
						}
					}
				}
			}
			return $data;
		}

		private function save_bdx($bdx_data, $qid = 0){
			$data = array();
			foreach ($bdx_data['subject_text'] as $key => $value) {
				if(!empty($value) && (!empty($bdx_data['checkbox_text'][$key][1]) || !empty($bdx_data['checkbox_text'][$key][2]) || !empty($bdx_data['checkbox_text'][$key][3]) || !empty($bdx_data['checkbox_text'][$key][4])) && (!empty($bdx_data['checkbox'][$key][1]) || !empty($bdx_data['checkbox'][$key][2]) || !empty($bdx_data['checkbox'][$key][3]) || !empty($bdx_data['checkbox'][$key][4]))){
					$data[$key]['subject_text'] = $value;
					$data[$key]['bdx_result'] = array("checkbox_text" => $bdx_data['checkbox_text'][$key], "checkbox" => $bdx_data['checkbox'][$key]);
				}
			}
			return $data;
		}

		function add(){
			$post_data = $this->input->post();
			if($post_data){
				// print_r($post_data);exit;
				$category_info = get_field_by_where("tk_categorys", "name", "id='{$post_data['category_id']}'");
				$area_info = get_field_by_where("areas", "area_name", "area_code='{$post_data['area_code']}'");
				$data['subject_id'] 		= intval($post_data['subject_id']);
				$data['chapter_id'] 		= intval($post_data['chapter_id']);
				$data['type'] 				= intval($post_data['type']);
				$data['difficulty_level'] 	= intval($post_data['difficulty_level']);
				$data['subject_text'] 		= $post_data['subject_text'];
				$data['category_id'] 		= $post_data['category_id'];
				$data['category_name'] 		= $category_info['name'];
				$data['area_code'] 			= $post_data['area_code'];
				$data['area_name'] 			= $area_info['area_name'];
				$data['analysis'] 			= $post_data['analysis'];
				$data['add_time'] 			= time();
				if($data['type'] == 1){
					if(empty($post_data['an_radio'])){
						show_message($_SERVER['HTTP_REFERER'], "请选择一个正确答案");exit;
					}
					if(empty($post_data['an_radio_text'])){
						show_message($_SERVER['HTTP_REFERER'], "请填写题目答案");exit;
					}
				}elseif($data['type'] == 2){
					if(empty($post_data['an_checkbox'])){
						show_message($_SERVER['HTTP_REFERER'], "请选择一个正确答案");exit;
					}
					if(empty($post_data['an_checkbox_text'])){
						show_message($_SERVER['HTTP_REFERER'], "请填写题目答案");exit;
					}
				}elseif($data['type'] == 3){
					if(empty($post_data['an_choice'])){
						show_message($_SERVER['HTTP_REFERER'], "请选择一个正确答案");exit;
					}
				}elseif($data['type'] == 4){
					if(empty($post_data['analys'])){
						show_message($_SERVER['HTTP_REFERER'], "至少需要一个子题干");exit;
					}
					$analys_data = $this->save_analys($post_data['analys']);
					if(!$analys_data){
						show_message($_SERVER['HTTP_REFERER'], "子题目是空的");
						exit;
					}
				}elseif($data['type'] == 7){
					if(empty($post_data['bdx'])){
						show_message($_SERVER['HTTP_REFERER'], "至少需要一个子题干");exit;
					}
					$bdx_data = $this->save_bdx($post_data['bdx']);
					if(!$bdx_data){
						show_message($_SERVER['HTTP_REFERER'], "子题目是空的");
						exit;
					}
				}elseif($data['type'] == 8 || $data['type'] == 9){
					if(empty($post_data['analysis'])){
						show_message($_SERVER['HTTP_REFERER'], "名词解释、简答题的解析内容不能为空");exit;
					}
				}
				$this->db->insert("tk_topics", $data);
				$insert_id = $this->db->insert_id();
				if($insert_id > 0){
					//插入答案数据
					if($data['type'] == 1){
						foreach ($post_data['an_radio_text'] as $radio_key => $radio_val) {
							$result_data['qid'] = $insert_id;
							$result_data['answer_text'] = $radio_val;
							$result_data['is_right'] =  ( $post_data['an_radio'] == $radio_key) ? 1 : 0;
							$this->db->insert("tk_topic_result", $result_data);
						}
					}elseif($data['type'] == 2){
						foreach ($post_data['an_checkbox_text'] as $checkbox_key => $checkbox_val) {
							$result_data['qid'] = $insert_id;
							$result_data['answer_text'] = $checkbox_val;
							$result_data['is_right'] =  ( isset($post_data['an_checkbox'][$checkbox_key])) ? 1 : 0;
							$this->db->insert("tk_topic_result", $result_data);
						}
					}elseif($data['type'] == 3){
						$result_data['qid'] = $insert_id;
						$result_data['answer_text'] = '正确';
						$result_data['is_right'] =  ( $post_data['an_choice'] == 1) ? 1 : 0;
						$this->db->insert("tk_topic_result", $result_data);

						$result_data['qid'] = $insert_id;
						$result_data['answer_text'] = '错误';
						$result_data['is_right'] = ( $post_data['an_choice'] == 2) ? 1 : 0;
						$this->db->insert("tk_topic_result", $result_data);
					}elseif($data['type'] == 4){
						foreach ($analys_data as $key => $topic_analys_data) {
							$topic_analys_data['analys']['qid'] = $insert_id;
							$this->db->insert("tk_topic_analys", $topic_analys_data['analys']);
							$aid = $this->db->insert_id();
							$topic_analys_result_data = array();
							foreach ($topic_analys_data['analys_result'] as $k => $topic_analys_result) {
								$topic_analys_result['aid'] = $aid;
								$topic_analys_result['qid'] = $insert_id;
								$topic_analys_result_data[] = $topic_analys_result;
							}
							$this->db->insert_batch("tk_topic_analys_result", $topic_analys_result_data);
						}
					}elseif($data['type'] == 7){
						$topic_bdx_result_data = array();
						foreach ($bdx_data as $key => $topic_bdx_data) {
							$topic_bdx_data['analys']['qid'] = $insert_id;
							$topic_bdx_data['analys']['subject_text'] = $topic_bdx_data['subject_text'];
							$this->db->insert("tk_topic_analys", $topic_bdx_data['analys']);
							$aid = $this->db->insert_id();
							
							foreach ($topic_bdx_data['bdx_result']['checkbox_text'] as $k => $answer_text) {
								$topic_bdx_result['aid'] = $aid;
								$topic_bdx_result['qid'] = $insert_id;
								$topic_bdx_result['answer_text'] = $answer_text;
								if(!empty($topic_bdx_data['bdx_result']['checkbox'][$k])){
									$topic_bdx_result['is_right'] = 1;
								}else{
									$topic_bdx_result['is_right'] = 0;
								}
								$topic_bdx_result_data[] = $topic_bdx_result;
							}
						}
						$this->db->insert_batch("tk_topic_analys_result", $topic_bdx_result_data);
					}
 					show_message($_SERVER['HTTP_REFERER']);
 				}else{
 					show_message($_SERVER['HTTP_REFERER'], "操作失败");
 				}
			}else{
				$this->load->model("Topics");
				$this->load->model("Areas");
				$this->load->model("Categorys");
				$subject = $this->Topics->get_subject();
				$province_list = $this->Areas->get_province_data();
				$cagetory_list = $this->Categorys->get_category_data();
				$this->smarty->assign("action", '添加题目');
				$this->smarty->assign("subject", $subject);
				$this->smarty->assign("province_list", $province_list);
				$this->smarty->assign("cagetory_list", $cagetory_list);
				$this->smarty->assign("post_url", '/topic/add');
				$this->smarty->display("topic/save.html");
			}
		}

		function edit($id = 0){
			$post_data = $this->input->post();
			if($post_data){
				$category_info 				= get_field_by_where("tk_categorys", "name", "id='{$post_data['category_id']}'");
				$area_info 					= get_field_by_where("areas", "area_name", "area_code='{$post_data['area_code']}'");
				$data['subject_id'] 		= intval($post_data['subject_id']);
				$data['chapter_id'] 		= intval($post_data['chapter_id']);
				$data['difficulty_level'] 	= intval($post_data['difficulty_level']);
				$data['category_id'] 		= $post_data['category_id'];
				$data['category_name'] 		= $category_info['name'];
				$data['area_code'] 			= $post_data['area_code'];
				$data['area_name'] 			= $area_info['area_name'];
				$data['analysis'] 			= $post_data['analysis'];
				$this->db->where('id', $post_data['id']);
 				$this->db->update('tk_topics', $data);
 				if($this->db->affected_rows() > 0){
 					show_message($_SERVER['HTTP_REFERER']);
 				}else{
 					show_message($_SERVER['HTTP_REFERER'], "操作失败");
 				}
			}else{
				if(intval($id) > 0){
					$topic_info = get_field_by_where("tk_topics", "*", "id='{$id}'");
					if(!$topic_info){
						show_message("/news/type_list", "数据不存在");exit;
					}
					$this->load->model("Topics");
					$this->load->model("Areas");
					$this->load->model("Categorys");
					$subject = $this->Topics->get_subject();
					$province_list = $this->Areas->get_province_data();
					$cagetory_list = $this->Categorys->get_category_data();
					// $topic_result = $this->Topics->get_topic_reuslt($id);
					$chapter_list_data = $this->get_chapter_list($topic_info['subject_id']);
					if($topic_info['type'] == 4 || $topic_info['type'] == 7){
						$topic_result = $this->Topics->get_topic_analys($id);
						if($topic_result){
							foreach ($topic_result as $key => $value) {
								$topic_result[$key]['result'] = $this->Topics->get_topic_analys_result($value['id']);
							}
						}
					}else{
						$topic_result = $this->Topics->get_topic_reuslt($id);
					}
					$letter = array('A','B','C','D');
					$this->smarty->assign("topic_info", $topic_info);
					$this->smarty->assign("action", '编辑题目');
					$this->smarty->assign("subject", $subject);
					$this->smarty->assign("province_list", $province_list);
					$this->smarty->assign("cagetory_list", $cagetory_list);
					$this->smarty->assign("chapter_list_data", $chapter_list_data);
					$this->smarty->assign("topic_result", $topic_result);
					$this->smarty->assign("letter", $letter);
					$this->smarty->assign("post_url", '/topic/edit');
					$this->smarty->display("topic/save.html");
				}else{
					show_message("/news", "非法操作");
				}
			}
		}

		function get_chapter_list($subject_id){
			$this->load->model("Topics");
			$conditions['subject_id'] = $subject_id;
			$chapter_list = $this->Topics->get_chapter($conditions);
			$chapter_list_data = array();
			$code = 0;
			if($chapter_list){
				foreach ($chapter_list as $key => $value) {
					if($value['parent_id'] == 0){
						$chapter_list_data[$value['id']] = $value;
						unset($chapter_list[$key]);
					}
				}

				foreach ($chapter_list as $key => $value) {
					if(isset($chapter_list_data[$value['parent_id']])){
						$chapter_list_data[$value['parent_id']]['sub'][$value['id']] = $value;
						unset($chapter_list[$key]);
					}
				}

				if($chapter_list AND $chapter_list_data){
					foreach ($chapter_list_data as $key => $value) {
						if(isset($value['sub'])){
							foreach ($value['sub'] as $k => $v) {
								foreach ($chapter_list as $k1 => $v1) {
									if($k == $v1['parent_id']){
										$chapter_list_data[$key]['sub'][$k]['sub'][$v1['id']] = $v1;
									}
								}
							}
						}
					}
				}
			}
			return $chapter_list_data;
		}

		function delete($ids = 0){
			if($_POST){
				$ids = $this->input->post('ids');
			}else{
				$ids = (array) $ids;
			}
			delete_data($ids, "tk_topics");
			if($this->db->affected_rows() > 0){
				show_message($_SERVER['HTTP_REFERER'], "删除成功");
			}else{
				show_message($_SERVER['HTTP_REFERER'], "删除失败，请重试！");
			}
		}

		function import(){
			$this->smarty->assign("action", '导入题目');
			$this->smarty->display("topic/import.html");
		}

		function ignore_get_subject_chapter(){
			$post_data = $this->input->post();
			if(is_ajax_request()){
				$chapter_list_data = $this->get_chapter_list_data($post_data['subject_id']);
				$code = 0;
				if($chapter_list_data){
					$code = 1;
				}
				echo json_encode(array("code" => $code, "chapter_list" => $chapter_list_data));
			}
		}

		function get_chapter_list_data($subject_id){
			$this->load->model("Topics");
			$conditions['subject_id'] = $subject_id;
			$chapter_list = $this->Topics->get_chapter($conditions);
			$chapter_list_data = array();
			$code = 0;
			$keys = array();
			if($chapter_list){
				foreach ($chapter_list as $key => $value) {
					if($value['parent_id'] == 0){
						$chapter_list_data[$value['id']] = $value;
						$keys[$value['id']] = 1;
						unset($chapter_list[$key]);
					}
				}

				foreach ($chapter_list as $key => $value) {
					if(isset($chapter_list_data[$value['parent_id']])){
						$chapter_list_data[$value['parent_id']]['sub'][$value['id']] = $value;
						unset($chapter_list[$key]);
					}
				}

				if($chapter_list AND $chapter_list_data){
					foreach ($chapter_list_data as $key => $value) {
						if($value['sub']){
							foreach ($value['sub'] as $k => $v) {
								foreach ($chapter_list as $k1 => $v1) {
									if($k == $v1['parent_id']){
										$chapter_list_data[$key]['sub'][$k]['sub'][$v1['id']] = $v1;
									}
								}
							}
						}
					}
				}
			}
			$data = array();
			if(count($keys) > 0){
				ksort($keys);
				foreach ($keys as $k => $v) {
					$data[$k] = $chapter_list_data[$k];
				}
			}
			return $data;
		}
	}
